/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package FEMShapeFunction1D;

import Integration.IFunction1D;

/**
 *
 * @author mark_o
 */
public class FEMShapeFunction1D2 implements IFunction1D{
    double xi;
    double h;

    public FEMShapeFunction1D2(double xi, double h) {
        this.xi = xi;
        this.h = h;
    }

    public double getH() {
        return h;
    }

    public double getXi() {
        return xi;
    }

    public double calculate(double x) {
        if((xi-h<x)&&(xi+h>x)){
            return (x-xi-h)*(x-xi+h)/( (-h*h) );
        }
        if((xi-2*h<x)&&(xi-h>=x)){
            return (x-xi)*(x-xi+h)/( (2*h*h) );
        }
        if((xi+h<=x)&&(xi+2*h>x)){
            return (x-xi-h)*(x-xi)/( (2*h*h) );
        }

        return 0;
    }

    public double calculateDerivaty(double x) {
        if((xi-h<x)&&(xi+h>x)){
            return (x-xi+h)/( (-h*h) +(x-xi-h)/( (-h*h) ));
        }
        if((xi-2*h<x)&&(xi-h>=x)){
            return (x-xi)/( (2*h*h) )+(x-xi+h)/( (2*h*h) );
        }
        if((xi+h<=x)&&(xi+2*h>x)){
            return (x-xi)/( (2*h*h) ) + (x-xi-h)/( (2*h*h) );
        }

        return 0;
    }

    public static void main(String[] args){
        //ArrayList<Double> list = new ArrayList<Double>();
        double a = 0;
        double b = 1;
        int N = 200;
        double h = (b-a)/N;
        //for (int i = 0; i <= N; i++) {
        //    list.add(a+h*i);
        //}
        FEMShapeFunction1D2 f = new FEMShapeFunction1D2(0.50, 0.125);
        for (int i = 0; i <= N; i++) {
            System.out.println((a+h*i)+"\t\t"+f.calculate(a+h*i));
        }

    }

}
